package cn.inno.admin;

import cn.inno.controller.BaseController;
import cn.inno.entitys.SysAdmin;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 管理员控制器
 *
 * @author LeungZengJian
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/adminList")
    @ResponseBody
    public String adminList() {
        JSONObject jsonObject = new JSONObject();
        try {
            SysAdmin admin = (SysAdmin) this.getAuth();
            jsonObject = this.adminService.adminList(admin.getAdminId());
        } catch (Exception e) {
            this.writeExceptionJson(jsonObject, e);
        }
        return this.writeJson(jsonObject);
    }

    @PostMapping("/adminAdd")
    @ResponseBody
    public String adminAdd(SysAdmin sysAdmin) {
        JSONObject jsonObject = new JSONObject();
        try {
            SysAdmin admin = (SysAdmin) this.getAuth();
            jsonObject = this.adminService.adminAdd(sysAdmin, admin.getAdminId());
        } catch (Exception e) {
            this.writeExceptionJson(jsonObject, e);
        }
        return this.writeJson(jsonObject);
    }

    @PostMapping("/adminDel")
    @ResponseBody
    public String adminDel(String adminIds) {
        JSONObject jsonObject = new JSONObject();
        try {
            SysAdmin admin = (SysAdmin) this.getAuth();
            jsonObject = this.adminService.adminDel(adminIds, admin.getAdminId());
        } catch (Exception e) {
            this.writeExceptionJson(jsonObject, e);
        }
        return this.writeJson(jsonObject);
    }

    @PostMapping("/adminUp")
    @ResponseBody
    public String adminUp(SysAdmin sysAdmin) {
        JSONObject jsonObject = new JSONObject();
        try {
            SysAdmin admin = (SysAdmin) this.getAuth();
            jsonObject = this.adminService.adminUp(sysAdmin, admin.getAdminId());
        } catch (Exception e) {
            this.writeExceptionJson(jsonObject, e);
        }
        return this.writeJson(jsonObject);
    }

    @PostMapping("/adminUpStatus")
    @ResponseBody
    public String adminUpStatus(SysAdmin sysAdmin) {
        JSONObject jsonObject = new JSONObject();
        try {
            SysAdmin admin = (SysAdmin) this.getAuth();
            jsonObject = this.adminService.adminUpStatus(sysAdmin, admin.getAdminId());
        } catch (Exception e) {
            this.writeExceptionJson(jsonObject, e);
        }
        return this.writeJson(jsonObject);
    }

    @GetMapping("/admin-edit.html")
    public String adminEdit(Model model, String adminId) {
        JSONObject jsonObject = new JSONObject();
        try {
            this.getAuth();
            jsonObject = this.adminService.adminEdit(adminId);
        } catch (Exception e) {
            this.writeExceptionJson(jsonObject, e);
        }
        model.addAttribute(KEY_DATA, jsonObject);
        return "/admin/admin-edit";
    }

}
